package hello

import (
	"context"
	"fmt"
	"protobuf-learn/proto"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type Service struct {
	proto.UnimplementedHelloServiceServer
}

func (s Service) SayHello(ctx context.Context, request *proto.SayHelloRequest) (*proto.SayHelloResponse, error) {
	//start := time.Now()
	// md, ok := metadata.FromIncomingContext(ctx)
	// if !ok || len(md.Get("x-request-id")) == 0 {
	// 	return nil, status.Error(codes.InvalidArgument, "x-request-id is required")
	// }
	// log.Printf("requst id: %s", md.Get("x-request-id")[0])
	if request.GetName() == "" {
		return nil, status.Error(codes.InvalidArgument, "name is required")
	}
	// headers := metadata.New(map[string]string{
	// 	"request-start-timestamp": start.String(),
	// })
	// if err := grpc.SendHeader(ctx, headers); err != nil {
	// 	return nil, status.Error(codes.Internal, "failed to send header")
	// }

	// trailers := metadata.New(map[string]string{
	// 	"request-end-timestamp": time.Now().String(),
	// })
	// if err := grpc.SetTrailer(ctx, trailers); err != nil {
	// 	return nil, status.Error(codes.Internal, "failed to send trailer")
	// }

	return &proto.SayHelloResponse{
		Message: fmt.Sprintf("hello %s", request.GetName()),
	}, nil
}
